﻿// -----------------------------------------------------------------------
// <copyright file="AIMoveEngine.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace JumpJump.AI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using JumpJump.Runtime;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AIMovementEngine
    {
        private List<Movement> _allPossibleMovement = new List<Movement>();

        private Chessboard _board;

        private List<MovementEstimator> _movementEstimatroList = new List<MovementEstimator>();

        private StartDomain _boardEstimator;

        private int _allMovementCount = 0;

        private int _curMoveScore = 0;

        public int AllMovementCount
        {
            get 
            {
                return _allMovementCount;
            }
        }

        public int CurMoveScore
        {
            get 
            {
                return _curMoveScore;
            }
        }

        public AIMovementEngine(Chessboard board)
        {
            this._board = board;
            this._boardEstimator = new StartDomain(_board);
        }

        public Movement TryToMove()
        {
            GetAllMovement();
            int maxScore = int.MinValue;
            Movement finalMove = null;

            Piece standAlonePiece = StartDomain.GetStandAlonePiece(_board);
            if (standAlonePiece != null)
            {
                List<Movement> allAlonePieceMovement = standAlonePiece.CreatAllMoveMent();
                _allMovementCount = allAlonePieceMovement.Count;
                foreach (Movement oneMove in allAlonePieceMovement)
                {
                    int curScore = EstiamteOneVisualMove(oneMove);
                    if (curScore > maxScore)
                    {
                        finalMove = oneMove;
                        maxScore = curScore;
                    }
                }
                finalMove.Source = MovementSource.Engine_LastStandAlonePiece;
                finalMove.Score = maxScore;
            }
            else
            {
                _allMovementCount = _allPossibleMovement.Count;
                foreach (Movement oneMove in _allPossibleMovement)
                {
                    int curScore = EstiamteOneVisualMove(oneMove);
                    if (curScore > maxScore)
                    {
                        finalMove = oneMove;
                        maxScore = curScore;
                    }
                }
                finalMove.Source = MovementSource.Engine_CommonMove;
                finalMove.Score = maxScore;
            }

            return finalMove;
        }

        private int EstiamteOneVisualMove(Movement oneMove)
        {
            int moveScore = MovementEstimator.Estimate(oneMove);

            _board.PieceMove(oneMove.UserID, oneMove.PieceID, oneMove.Target);
            int boardScore = _boardEstimator.Estimate();

            _board.PieceMove(oneMove.UserID, oneMove.PieceID, oneMove.From);

            int allScore = moveScore + boardScore;
            return allScore;
        }

        private void GetAllMovement()
        {
            _allPossibleMovement.Clear();
            foreach (Movement move in _board.GetOnePlayerAllValidMovement(Chessboard.PrickID))
            {
                if (MovementEstimator.IsAGoodMove(move))
                {
                    _allPossibleMovement.Add(move);
                }
            }
        }
    }
}
